package com.woniuxy.thred.state;

/**
 * description: AllSate <br>
 * date: 2022/2/5 6:56 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 观察线程的状态
 */
public class AllSate {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(100);
                    System.out.println("....");
                } catch (InterruptedException e) {
                    e.printStackTrace();

                }
            }

        });

        //观察状态
        Thread.State state = t.getState();
        //new
        System.out.println(state);

        t.start();
        //RUNNABLE
        state = t.getState();
        System.out.println(state);

//        while (state!=Thread.State.TERMINATED){
//            try {
//                Thread.sleep(200);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            //TIMED_WAITING
//            state = t.getState();
//            System.out.println(state.toString());
//        }


        while (true) {
            int num = Thread.activeCount();
            System.out.println(num);
            if (num == 2) {
                break;
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //TIMED_WAITING
            state = t.getState();
            System.out.println(state);
        }

        //TERMINATED
//        state = t.getState();
//        System.out.println(state.toString());
    }
}
